package com.paperobot.action;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.paperobot.common.util.PageUtil;
import com.paperobot.common.util.CipherUtil;
import com.paperobot.model.Admin;
import com.paperobot.service.PaperobotService;

public class UserAction extends BaseAction{

	private static final long serialVersionUID = 1516834338392982332L;
	private PaperobotService paperobotService;
	private Admin admin; 
	private List<Admin> adminList = new ArrayList<Admin>();
	private PageUtil pageUtil = new PageUtil();
	private final int valid = 1;
	
	@Resource
	public void setPaperobotService(PaperobotService paperobotService) {
		this.paperobotService = paperobotService;
	}
	
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public List<Admin> getAdminList() {
		return adminList;
	}
	public void setAdminList(List<Admin> adminList) {
		this.adminList = adminList;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	
	public String admin(){
		return "login";
	}
	
	public String login(){
	
        try {
			String userName = request.getParameter("username");
			String passWord = request.getParameter("password");
      
			Admin user = paperobotService.getAdmin(userName);
			if(null == user || user.getStatus() != valid){
				request.setAttribute("result", "该用户不存在！");
				return ERROR;
			}
			if(user.getPassword().equals(CipherUtil.endodePassword(passWord))){
				
				request.getSession().setAttribute("loginUser",user);
				
				logger.info("用户: "+ user.getUserName() + " 成功登录系统。");
				return SUCCESS;
			}else{
				logger.info("用户: "+ user.getUserName() + " 登录密码错误。");
				request.setAttribute("result", "密码输入错误！");
				return ERROR;
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String logout(){
		
		try {
			request.getSession().removeAttribute("loginUser");
			request.getSession().invalidate();
			
			response.setDateHeader("Expires", 0);   
			response.setHeader("Cache-Control", "no-cache");   
			response.setHeader("Pragma", "no-cache");  

			return SUCCESS;
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String addAdmin(){

        try {
			String userName = request.getParameter("username");
			String passWord = request.getParameter("password");
			String type = request.getParameter("type");
			String status = request.getParameter("status");
			     
			logger.info("userName:  " + userName + "  passWord:  " + passWord + " type:  " + type + "  status: " + status );

			Admin user = new Admin();
			user.setUserName(userName);
			user.setPassword(CipherUtil.endodePassword(passWord));
			user.setType(Integer.parseInt(type));
			user.setStatus(Integer.parseInt(status));
			
			if(paperobotService.addAdmin(user) > 0){
				logger.info("用户名: 【"+ user.getUserName() + "】 添加用户成功。");
				request.setAttribute("result", "添加用户成功！");
				return SUCCESS;
			}else{
				logger.info("用户名: 【"+ user.getUserName() + "】 添加用户失败。");
				request.setAttribute("result", "添加用户失败！");
				return ERROR;
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
        
	}

	
	@SuppressWarnings("unchecked")
	public String listAdmin(){
		try {
			String pagetype = request.getParameter("pagetype");
			if(pagetype == null){
				pagetype = "first";
			}
			
			String currentPage = request.getParameter("currentPage");
			if(currentPage != null){
				pageUtil.setCurrentPage(Integer.parseInt(currentPage));
			}
			List<Admin> allAdminList = paperobotService.getAdminList();
			adminList = pageUtil.getCurrentPageRows(pagetype, allAdminList.size(), allAdminList);
			
			return SUCCESS;
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String updateAdmin(){
		try {
			String userName = request.getParameter("username");
			admin = paperobotService.getAdmin(userName);
			return "update";
			
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String deleteAdmin(){
		try {
			String adminId = request.getParameter("adminId");
			int rs = paperobotService.deleteAdmin(Long.parseLong(adminId));
			if(rs > 0){
				return "delete";
			}
			return ERROR;
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
	
	public String saveAdmin(){
		
		try {
			String userName = request.getParameter("username");
			String newpassword = request.getParameter("newpassword");
			String qrNewpassword = request.getParameter("qrNewpassword");
			String type = request.getParameter("type");
			String status = request.getParameter("status");
			
			if(!newpassword.equals(qrNewpassword)){
				request.setAttribute("result", "两次输入的密码不正确！");
				return ERROR;
			}
			
			Admin user = paperobotService.getAdmin(userName);
			if(null == user){
				request.setAttribute("result", "用户名不存在！");
				return ERROR;
			}

			user.setPassword(CipherUtil.endodePassword(newpassword));
			user.setType(Integer.parseInt(type));
			user.setStatus(Integer.parseInt(status));
			
			logger.info("userName:  " + userName + "  passWord:  " + newpassword + " type:  " + type + "  status: " + status );

			if(paperobotService.updateAdmin(user) > 0){
				logger.info("用户: "+ user.getUserName() + " 更新成功。");
				request.setAttribute("result", "更新用户成功！");
			
				return SUCCESS;
			}else{
				logger.info("用户: "+ user.getUserName() + " 更新失败。");
				request.setAttribute("result", "更新用户失败！");
				return ERROR;
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}

	public String saveUser(){
		
		try {
			String userName = request.getParameter("username");
			String newpassword = request.getParameter("newpassword");
			String qrNewpassword = request.getParameter("qrNewpassword");
			String type = request.getParameter("type");
			String status = request.getParameter("status");
			
			if(!newpassword.equals(qrNewpassword)){
				request.setAttribute("result", "两次输入的密码不正确！");
				return ERROR;
			}
			
			Admin user = paperobotService.getAdmin(userName);
			if(null == user){
				request.setAttribute("result", "用户名不存在！");
				return ERROR;
			}

			user.setPassword(CipherUtil.endodePassword(newpassword));
			user.setType(Integer.parseInt(type));
			user.setStatus(Integer.parseInt(status));
			
			logger.info("userName:  " + userName + "  passWord:  " + newpassword + " type:  " + type + "  status: " + status );

			if(paperobotService.updateAdmin(user) > 0){
				logger.info("用户: "+ user.getUserName() + " 更新成功。");
				request.setAttribute("result", "更新用户成功！");
			
				return SUCCESS;
			}else{
				logger.info("用户: "+ user.getUserName() + " 更新失败。");
				request.setAttribute("result", "更新用户失败！");
				return ERROR;
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
			return ERROR;
		}
	}
}


